package cn.com.yang.net;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import cn.com.yang.utils.DateUtil;
import cn.com.yang.utils.RSAUtil;

/**
 * 礼品请求报文测试
 * @author yangyijin
 *
 */
public class GiftRestFulApiHTTPS {
	
	/**
	 * 德众下单
	 * 
	 */
	@Test
	public void testOrder_DEZHONG() {
		RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "DZ-mfX3QAbIHW2k840P";
		String mchtId="DZ-FHUKECEkSssWJmHW";
		String orderId ="D20220317151233142340000000004";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		System.out.println("签名信息："+TranSign);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrgId","0133");
		params.put("OrgName","平安支行");
		params.put("Buyer","苏杨");
		params.put("Phone","13126164297");
		params.put("RcvAddr",new String[] {"河北省","石家庄","桥西区"});
		params.put("AddrDtl","平安大街79号");
		params.put("TotalPrice",198.00 + 165.0);
		List<Map> list = new ArrayList();
		Map<String, Object> prod1 = new HashMap<String, Object>();
		prod1.put("ProdId","P2022030900007224");
		prod1.put("ProdName","福临门东北优质大米2.5kg");
		prod1.put("Price",16.50);
		prod1.put("Num",10);
		list.add(prod1);
		Map<String, Object> prod2 = new HashMap<String, Object>();
		prod2.put("ProdId","P2022030900007237");
		prod2.put("ProdName","美的电压力锅5L");
		prod2.put("Price",198.00);
		prod2.put("Num",1);
		list.add(prod2);
		params.put("OrderInfo",list);
		
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		//测试RSA
		String pubKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7s2Yw72ToTqwOec0Pyutp29Ia9Lmx1a+Z9YQgiT1VSD7QyGUqPlzckYrLT5Oz6RvG0G4zFobDCoftuQD5pIH/KgoAybt5zyppMdziNUdQLjSYXLp3wxa2qEENylwlvMTamEMfdkLAEG7kGZv0tckwHs8zFxJofX41vCIqt6iSA+p4y7WKzbP4ICAla1AsOLA1bUkpOzmcWSrHHHZMuVDGJJEjK3dWfokjLKzXmtA9JbGyUzCCjwznsRQI9szO3l0lE2q+fJ3MF4njtrjxPz+7uhwaRoSlMU3rLL/c7t5HV7VFXBdY47a7TwqX8hyNUWO06STvHc7lxj2LBZl9woD6kGxThDSROV7daTTs8J4ZPMn+CbLpcWlpdjR9aqNF+QuPiy+wN+kHEMHS6RfPP0sbDHqlP91JrXg5y67kJP2DVm3TPCGvnFJpVeMMnxymXe6DAqi7TXDVA3ahPuuzfKhETlfV6AMRz02WqDS5XfTLPTJYEpyKmwCV/G1GK5ug70Os5Wqs3DtBqC2i4zuCdUQhJtIMUkpnuBeMUbQBvuoUAmOLSXP2FImb4fNuv8fTP/zOyjoNs1j9rcSM4su/EQeYkzUjAyjoy8xYt4DEE4PjLMSUJX2BueHGPzWJdSvCblYlfWXOtnjFI8IhQygZEz67jS1EEOWfhPpxMhRABulHxkCAwEAAQ==";
		try {
			rsaStr = RSAUtil.encrypt(rsa,pubKey,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange("http://hbyhtest.duobashuzi.com/api/HbOrder/order", HttpMethod.POST,entity,String.class);
			
			System.out.println("德众下单响应："+resultEntity.toString());
			System.out.println("德众下单响应body："+resultEntity.getBody().toString());
			System.out.println("德众下单响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 德众签收
	 */
	@Test
	public void testSign_DEZHONG() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "DZ-mfX3QAbIHW2k840P";
		String mchtId="DZ-FHUKECEkSssWJmHW";
		String orderId ="D20220317151233142340000000004";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrderNum","20220321104109609804");
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		//测试RSA
		String pubKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7s2Yw72ToTqwOec0Pyutp29Ia9Lmx1a+Z9YQgiT1VSD7QyGUqPlzckYrLT5Oz6RvG0G4zFobDCoftuQD5pIH/KgoAybt5zyppMdziNUdQLjSYXLp3wxa2qEENylwlvMTamEMfdkLAEG7kGZv0tckwHs8zFxJofX41vCIqt6iSA+p4y7WKzbP4ICAla1AsOLA1bUkpOzmcWSrHHHZMuVDGJJEjK3dWfokjLKzXmtA9JbGyUzCCjwznsRQI9szO3l0lE2q+fJ3MF4njtrjxPz+7uhwaRoSlMU3rLL/c7t5HV7VFXBdY47a7TwqX8hyNUWO06STvHc7lxj2LBZl9woD6kGxThDSROV7daTTs8J4ZPMn+CbLpcWlpdjR9aqNF+QuPiy+wN+kHEMHS6RfPP0sbDHqlP91JrXg5y67kJP2DVm3TPCGvnFJpVeMMnxymXe6DAqi7TXDVA3ahPuuzfKhETlfV6AMRz02WqDS5XfTLPTJYEpyKmwCV/G1GK5ug70Os5Wqs3DtBqC2i4zuCdUQhJtIMUkpnuBeMUbQBvuoUAmOLSXP2FImb4fNuv8fTP/zOyjoNs1j9rcSM4su/EQeYkzUjAyjoy8xYt4DEE4PjLMSUJX2BueHGPzWJdSvCblYlfWXOtnjFI8IhQygZEz67jS1EEOWfhPpxMhRABulHxkCAwEAAQ==";
		try {
			rsaStr = RSAUtil.encrypt(rsa,pubKey,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange("http://hbyhtest.duobashuzi.com/api/HbOrder/signFor", HttpMethod.POST,entity,String.class);
			System.out.println("德众签收响应："+resultEntity.toString());
			System.out.println("德众签收响应body："+resultEntity.getBody().toString());
			System.out.println("德众签收响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 平南下单
	 */
	@Test
	public void testOrder_PINGNAN() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		// 授权码：9xAvjBbfDORd6tY8
		//商户号：001117560760
		String authCode = "9xAvjBbfDORd6tY8";
		String mchtId="001117560760";
		String orderId ="D20220317151233142340000000001";
		headers.set("TranSign",DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId));
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrgId","0133");
		params.put("OrgName","平安支行");
		params.put("Buyer","苏杨");
		params.put("Phone","13126164297");
		params.put("RcvAddr",new String[] {"河北省","石家庄","桥西区"});
		params.put("AddrDtl","平安大街79号");
		params.put("TotalPrice",198.00 + 165.0);
		List<Map> list = new ArrayList();
		Map<String, Object> prod1 = new HashMap<String, Object>();
		prod1.put("ProdId","P2022030900007204");
		prod1.put("ProdName","福临门三江家宴米");
		prod1.put("Price",29.00);
		prod1.put("Num",10);
		list.add(prod1);
		Map<String, Object> prod2 = new HashMap<String, Object>();
		prod2.put("ProdId","P2022030900007211");
		prod2.put("ProdName","福临门黄金小黑葵葵花仁油");
		prod2.put("Price",54.00);
		prod2.put("Num",1);
		list.add(prod2);
		params.put("OrderInfo",list);
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		//测试RSA
		String pubKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAoZCUcbMVJ/VC42u9+W7EjWaH2gF/vseZMvNhRoj0vB0Ete9FdkRG50M9KQOnC3Derd7riGLPeAPZmvHFWOGIGH2j0Ks8VvxDZ9m1G1pOASxsLvcwdktD4plbFH1ak9WHhffIwvjXi6MRUseIRj3VSTSujrwLJ8eilB5GZFyviaw27YY4l2mGAPGqZuLs5qkeeqYO0KJLdsoxf1Vje0ci3PTZkX4pyWGHbVbGSRZ9H2NmEX92kiisWWk5jdK17Gdwn0A8ZvCJuoVidvoFkp1Q5NKRt6Sh225YLy9ZGLug/wEy/UayI45PtkuJm2xlnx/nI7viCDTrO7EUTsEnxmbLI2/EdYp0y07zg8pA1dvhe5DWgSr08zcNT4UIDoO7zbD3D0XkN7VwDZiTtWeYHWvv1q4Ja47MCZGw4dKTazuM4JUc6X6MESJ4fY1frELxu+kSiqm9+xyr9+P4XcRJJ3ESqZoMzDipmx93R8ajPU8jr+T2tQ3DCCq33dpq9ZHjQgUog8tGUhPmrGRSTyJladstSoD7wZRy/SxPDluIeDqot6ZOhm2rBd+q+1t9LYNNL30vK1eCbOR9i1i+VIrG2m+GvySGLrRaczIn9WYnoH6Zh4iw1Ja+U+xiXZzAeMKunp0ZSecaQMeLFMsC0XH1QHzIRTWzn/SOQ/QNBMnul37Zf0sCAwEAAQ==";
		try {
			rsaStr = RSAUtil.encrypt(rsa,pubKey,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange("http://domo.huicaib2b.com/api/order.php", HttpMethod.POST,entity,String.class);
			System.out.println("平南下单响应："+resultEntity.toString());
			System.out.println("平南下单响应body："+resultEntity.getBody().toString());
			System.out.println("平南下单响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 平南签收
	 */
	@Test
	public void testSign_PINGNAN() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "9xAvjBbfDORd6tY8";
		String mchtId="001117560760";
		String orderId ="D20220317151233142340000000001";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrderNum","SH20220322190811746088");
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		//测试RSA
		String pubKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAoZCUcbMVJ/VC42u9+W7EjWaH2gF/vseZMvNhRoj0vB0Ete9FdkRG50M9KQOnC3Derd7riGLPeAPZmvHFWOGIGH2j0Ks8VvxDZ9m1G1pOASxsLvcwdktD4plbFH1ak9WHhffIwvjXi6MRUseIRj3VSTSujrwLJ8eilB5GZFyviaw27YY4l2mGAPGqZuLs5qkeeqYO0KJLdsoxf1Vje0ci3PTZkX4pyWGHbVbGSRZ9H2NmEX92kiisWWk5jdK17Gdwn0A8ZvCJuoVidvoFkp1Q5NKRt6Sh225YLy9ZGLug/wEy/UayI45PtkuJm2xlnx/nI7viCDTrO7EUTsEnxmbLI2/EdYp0y07zg8pA1dvhe5DWgSr08zcNT4UIDoO7zbD3D0XkN7VwDZiTtWeYHWvv1q4Ja47MCZGw4dKTazuM4JUc6X6MESJ4fY1frELxu+kSiqm9+xyr9+P4XcRJJ3ESqZoMzDipmx93R8ajPU8jr+T2tQ3DCCq33dpq9ZHjQgUog8tGUhPmrGRSTyJladstSoD7wZRy/SxPDluIeDqot6ZOhm2rBd+q+1t9LYNNL30vK1eCbOR9i1i+VIrG2m+GvySGLrRaczIn9WYnoH6Zh4iw1Ja+U+xiXZzAeMKunp0ZSecaQMeLFMsC0XH1QHzIRTWzn/SOQ/QNBMnul37Zf0sCAwEAAQ==";
		try {
			rsaStr = RSAUtil.encrypt(rsa,pubKey,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange("http://domo.huicaib2b.com/api/finish.php", HttpMethod.POST,entity,String.class);
			System.out.println("平南签收响应："+resultEntity.toString());
			System.out.println("平南签收响应body："+resultEntity.getBody().toString());
			System.out.println("平南签收响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
